
import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/util/assets_util.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/drop_down_button_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/input_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/tab_widget.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';

import 'details/farm_details_view.dart';
import 'farm_logic.dart';
import 'search/farm_search_view.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/11/25 15:38
/// filePath： lib/page/farm/farm_view.dart
/// desc: 农场
/// <summary>
///
class FarmPage extends StatefulWidget {
  @override
  _FarmPageState createState() => _FarmPageState();
}

class _FarmPageState extends State<FarmPage> {
  final logic = Get.put(FarmLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.getImageList();
    logic.getType();
    logic.getCommodityList();
    logic.getLocation();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FarmLogic>(
      builder: (controller) {
        return baseScaffoldWidget(
          onRefresh: ()async{
            // logic.getLocation();
            logic.getCommodityList();
          },
          onLoad: () async{
            logic.getCommodityList(load: true);
          },
          child: ListView(
            shrinkWrap: true,
            padding: EdgeInsets.all(0),
            children: [
              _header(),

              _tabBar(),

              _rowMenu(),

              _listBody(),
            ],
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<FarmLogic>();
    logic.closeLocation();
    super.dispose();
  }

  Widget _header() {
    return Container(
      width: Get.width,
      child: Stack(
        children: [
          // 背景
          Container(
            width: Get.width,
            height: (Get.width - 30) / (16/9),
            decoration: BoxDecoration(
                image: DecorationImage(image: AssetImage(getLocalImg("head_bg.png")),fit: BoxFit.cover)
            ),
          ),
          Center(
            child: Container(
              width: Get.width - 30,
              child:Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  // 顶部
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                      children: [
                        Container(
                          child: Row(
                            children: [
                              Icon(Icons.location_on_outlined,color: Colors.white,size: 18,),
                              Expanded(child: InkWidget.text("${logic.local}     ",color: Colors.white,onTap: () async{
                                await CityPickers.showCityPicker(
                                  context: context,
                                  height: 280,
                                  confirmWidget: Text("确定"),
                                  cancelWidget: Text("取消"),
                                  barrierDismissible: false,
                                ).then((value){
                                  logic.setLocal( value!.provinceName! + value.cityName!);
                                });
                              }),),
                            ],
                          ),
                          width: 120,
                        ),
                        Expanded(child: textFieldWidget(
                            boxBorder: Border.all(color: Colors.white),
                            hintText: logic.searchKey == ""?"搜索喜欢的土地类型":logic.searchKey,
                            hintStyle: TextStyle(color: Colors.white),
                            enabled: false,
                            prefixIcon: Padding(
                              padding: EdgeInsets.only(left: 8,),
                              child: Icon(Icons.search,color: Colors.white,),
                            ),
                            onTap: (){
                              Get.to(()=>FarmSearchPage())!.then((value) {
                                if(value != null)logic.searchKey = value;
                                logic.getCommodityList();
                              });
                            }
                        ),),
                      ],
                    ),
                  ),

                  // 轮播图
                  SizedBox(
                    height: (Get.width - 30) / (16/9),
                    width: Get.width - 30,
                    child: logic.images!.length == 0 ? SizedBox():  ClipRRect(
                      child:  Swiper(
                        itemBuilder: (ctx,index){
                          return InkWidget.imgNet("${logic.images![index]}",fit: BoxFit.cover,width: Get.width - 30,height: (Get.width - 30) / (16/9),);
                        },
                        pagination: SwiperPagination(
                          builder: DotSwiperPaginationBuilder(
                            color: Colors.grey,
                            activeColor: Colors.black,
                            size: 9.0,
                            activeSize: 12.0,
                          ),
                        ),     //小圆
                        itemCount: logic.images!.length,
                        autoplay: true,
                      ),
                      borderRadius: BorderRadius.circular(10),
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// todo：分类选择栏
  _tabBar() {
    return tab(
        height: 60,
        tabs: logic.type,
        currentIndex: logic.currentIndex,
        mainAxisAlignment: MainAxisAlignment.start,
        onTap: (val){
          logic.setCurrentIndex(val);
        }
    );
  }

  Widget _rowMenu() {
    return rowBaseWidget(
      margin: EdgeInsets.all(15),
      padding: EdgeInsets.all(0),
      showLine: false,
      left: Container(
        decoration: BoxDecoration(
          border: Border(left: BorderSide(color: Colors.green,width: 2),),
        ),
        child: InkWidget.text(" 农场列表",color: Colors.green),
      ),
      right: DropDownButton(
        hin: logic.downButtonModel?.name ?? "排序",
        data: logic.listDropDownButtonModel,
        onTap: (v){
          logic.setDrop(v);
          logic.getCommodityList();
        },
      ),
    );
  }

  Widget _listBody() {
    return Container(
      child: Wrap(
        alignment: WrapAlignment.spaceBetween,
        runSpacing: 10,
        children: List.generate(logic.commodityListDataContent!.length, (index){
          double w = ( Get.width - 30 - 10) / 2;
          return InkWell(
            child: Container(
              width: w,
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5),
                  border: Border.all(color: Colors.green,width: 0.7),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.shade300,
                      spreadRadius: 1.5,
                      blurRadius: 2,
                    ),
                  ]
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  InkWidget.imgNet("${logic.commodityListDataContent![index].masterPic}",borderRadius: BorderRadius.circular(5),height: w, width: w,),
                  InkWidget.title("${logic.commodityListDataContent![index].commodityName}",color: Colors.grey.shade800,maxLines: 2),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      InkWidget.title("￥${logic.commodityListDataContent![index].price}",maxLines: 1,color: Colors.green,fonSize: 16),
                      InkWidget.text("已售${logic.commodityListDataContent![index].soldNum}",maxLines: 1,color: Colors.grey,fonSize: 13),
                    ],
                  ),
                ],
              ),
            ),
            onTap: (){
              Get.to(()=>FarmDetailsPage(shopId: logic.commodityListDataContent![index].shopId,farmId: logic.commodityListDataContent![index].commodityId,));
            },
          );
        }),
      ),
      padding: EdgeInsets.symmetric(horizontal: 15),
    );
  }


}